package main

import (
	"fmt"
	"time"
)

func main() {
	request := make(chan int, 5)
	for i := 1; i <= 5; i++ {
		request <- i
	}
	// close is used for prevent writing in channel
	close(request)

	limiter := time.Tick(200 * time.Millisecond)
	for req := range request{
		<-limiter // 200ms block（wait）
		fmt.Println("request ",req,time.Now())
	}

	// burst request
	burstLimiter := make(chan time.Time, 3)
	for i := 0; i < 3; i++ {
		burstLimiter<-time.Now()
	}
	go func() {
		for t := range time.Tick(200 * time.Millisecond){	
			burstLimiter <- t
		}
	}()

	// simulate
	burstyRequest := make(chan int, 5)
	for i := 1; i <= 5; i++ {
		burstyRequest<-i
	}
	close(burstyRequest)
	for req := range burstyRequest{
		<-burstLimiter
		fmt.Println("request ",req,time.Now())
	}
}
